import request from '@/utils/request'

// 上次API调用的时间戳
let lastCallTime = 0;
const THROTTLE_TIME = 500; // 500毫秒内不重复调用

// 查询生产线列表（按车间ID过滤）
export function getProductionLinesByWorkshop(workshopId) {
  console.log("查询指定车间的生产线，车间ID:", workshopId);
  
  // 节流处理
  const now = Date.now();
  if (now - lastCallTime < THROTTLE_TIME) {
    console.log("API调用过于频繁，跳过此次调用");
    return Promise.reject(new Error("操作过于频繁，请稍后再试"));
  }
  lastCallTime = now;
  
  const data = {
    basicWorkshop: {
      workshop_id: workshopId
    },
    is_delete: 0
  };

  return request({
    url: '/ldzl-basic/prt/find',
    method: 'post',
    data: data
  }).then(response => {
    console.log("获取生产线响应:", response);
    return response;
  }).catch(error => {
    console.error("获取生产线错误:", error);
    throw error;
  });
} 

// 查询生产线列表（按车间ID过滤）
export function listLineByWorkshop(workshopId, pageSize = 100) {
  console.log("查询指定车间的生产线列表，车间ID:", workshopId);
  
  // 节流处理
  const now = Date.now();
  if (now - lastCallTime < THROTTLE_TIME) {
    console.log("API调用过于频繁，跳过此次调用");
    return Promise.reject(new Error("操作过于频繁，请稍后再试"));
  }
  lastCallTime = now;
  
  return request({
    url: '/ldzl-basic/prt/list',
    method: 'post',
    params: {
      pageNum: 1,
      pageSize: pageSize
    },
    data: {
      factoryId: workshopId,
      is_delete: 0
    }
  }).then(response => {
    console.log("获取生产线列表响应:", response);
    return response;
  }).catch(error => {
    console.error("获取生产线列表错误:", error);
    throw error;
  });
} 